/*
 * $('input').suggestion();
 * <input type="text" class="text" data-suggestion="suggestion_logindlg" />
 * suggestion_logindlg 为自定义下拉列表的样式
 */

(function($) {
    $.fn.suggestion = function() {
        var _timer = null;
        var suggestion_list = $("#suggestion_list");
        var mailType = function(type, val) {
            type = type || '';
            val = val || '';
            if (val == '') {
                suggestion_list.hide();
                return false;
            }
            var html = '';
            var mails = '126.com,163.com,qq.com,sina.com,hotmail.com.cn,yahoo.com,shou.cn'.split(',');
            var mails_result = mails;
            if (type != '') {
                mails_result = [];
                for (var i = 0; i < mails.length; i++) {
                    if (mails[i].indexOf(type) > -1) {
                        mails_result.push(mails[i]);
                    }
                }
            }
            $.each(mails_result, function() {
                html += '<li data-address="' + this + '" unselectable="on"><span>' + val + '</span>@' + this + '</li>';
            });
            if (html == '') {
                suggestion_list.attr('type', type).html(html);
                suggestion_list.unbind().hide();
            } else {
                html = '<li class="none">请选择邮箱类型</li>' + html;
                suggestion_list.attr('type', type).html(html);
                var list = suggestion_list.find('li');
                if (list.length > 2) {
                    list.hover(function() {
                        $(this).toggleClass('cur');
                    });
                } else {
                    list.unbind().eq(1).attr('class', 'cur');
                }
                suggestion_list.fadeIn(200);
            }
        };
        if (!suggestion_list.length) {
            suggestion_list = $('<ul class="suggestion" style="display:none;" unselectable="on"></ul>').appendTo(document.body);
            suggestion_list.bind('click', function(e,focus) {
            	  e.preventDefault();
                var input = suggestion_list.data('input');
                var target = $(e.target);
                if (target.is('li') || target.parent().is('li')) {
                    target = target.is('li') ? target : target.parent();
                    if (input.length && target.find('span').length) {
                        input.val(target.find('span').text() + '@' + target.attr('data-address')).trigger('blur');
                        suggestion_list.fadeOut(200);
                        		setTimeout(function(){
                        	   		input.focus();
                           		},1);
                    }
                } else {
                    input.length && input.focus();
                }
            });
            $(document).bind('keydown.suggestion', function(e) {
                if (suggestion_list.css('display') == 'block') {
                    if ((e.keyCode == 38 || e.keyCode == 40) && suggestion_list.find('li').length > 2) { //上下箭头移动
                        var list = suggestion_list.find('li');
                        var length = list.length;
                        var cur = list.filter('.cur');
                        if (!cur.length) {
                            list.eq(1).addClass('cur');
                        } else {
                            var index = list.index(cur);
                            e.keyCode == 40 ? index++ : index--;
                            if (index < 1)
                                index = length - 1;
                            if (index >= length)
                                index = 1;
                            cur.removeClass('cur');
                            list.eq(index).addClass('cur');
                        }
                    } else if (e.keyCode == 13 || e.keyCode == 9) {//回车键或tab
                        var cur = suggestion_list.find('li.cur');
                        if (cur.length) {
                        	if(e.keyCode == 9){
                        		var input = suggestion_list.data('input');
                        		if (input.length && cur.find('span').length) {
                                    input.val(cur.find('span').text() + '@' + cur.attr('data-address'));
                                    suggestion_list.fadeOut(200);
                        		}
                        	}else{
                        		cur.trigger('click');
                        	}
                            
                        }
                    }


                }
                ;
            });
        }


        return this.each(function() {
            $(this).unbind('focusin').bind('focusin', function() {
                suggestion_list.data('input', $(this));
                var pos = $(this).offset();
                suggestion_list.css({
                    left: pos.left,
                    top: pos.top + $(this).height()
                }).attr('class', 'suggestion ' + $(this).attr('data-suggestion'));
            }).unbind('focusout').bind('focusout', function() {
                suggestion_list.fadeOut(200);
            });
            $(this).unbind('input propertychange').bind('input propertychange', function(e) {
                
                if (e.originalEvent && e.originalEvent.propertyName && e.originalEvent.propertyName != 'value') { //取到原始的event，判断是否修改了值
                    return;
                }
                var val = $.trim($(this).val());
                var type = '';
                if (val.indexOf('@') > -1) {
                    type = val.substr(val.indexOf('@') + 1, val.length);
                    val = val.substr(0, val.indexOf('@'));
                }
                mailType(type, val);
            });
        });
    };
})(jQuery);